package com.hnzq.cloud.apiImpl;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.hnzq.cloud.OrderAPI;
import com.hnzq.cloud.api.BalanceApi;
import com.hnzq.cloud.api.FreGoodsAPI;
import com.hnzq.cloud.api.SiteQueryApi;
import com.hnzq.cloud.balanceParam.UpdateBalanceParam;
import com.hnzq.cloud.dto.*;
import com.hnzq.cloud.model.Result;
import com.hnzq.cloud.param.*;
import com.qnzq.cloud.api.WarehouseOrderQueryApi;
import com.qnzq.cloud.dto.ListOderDto;
import com.qnzq.cloud.param.FindListOrderParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@RestController
@RequestMapping("/warerhouseOder")
public class Api implements WarehouseOrderQueryApi {
    @Resource
    private SiteQueryApi siteQueryApi;
    @Resource
    private OrderAPI orderAPI;
    //根据站点区域id查所有订单；
    @GetMapping("/findOderBySiteId")
    @Override
    public Result<ListOderDto> findOderBySiteId(FindListOrderParam findListOrderParam) {
        AllSiteDto allSiteDto = siteQueryApi.findAllSite(findListOrderParam.getSiteAreaId()).getData();
        List<OneSiteDto> list = allSiteDto.getSiteDtoList();
        ArrayList<Integer> listSiteIds = new ArrayList<>();
        //将门店id取出来放入集合中
        for (int i=0;i<list.size();i++){
            listSiteIds.add(list.get(i).getSiteId());
        }
        FindOrderBySiteIdListParam findOrderBySiteIdListParam = new FindOrderBySiteIdListParam();
        findOrderBySiteIdListParam.setEndTime(findListOrderParam.getEndTime());
        findOrderBySiteIdListParam.setStartTime(findListOrderParam.getStartTime());
        findOrderBySiteIdListParam.setSiteIds(listSiteIds);
        Object data = orderAPI.findOrderBySiteIdList(findOrderBySiteIdListParam).getData();
        ObjectMapper mapper = new ObjectMapper();
        PageOrdersDto pageOrdersDto = mapper.convertValue(data, new TypeReference<PageOrdersDto>() {
        });
        List<OrderDto> orderDtoList = pageOrdersDto.getOrderDtoList();
        //返回的当天该站点区域的所有订单
        return Result.newInstance().setData(orderDtoList);
    }
    //根据门店id该查询所有配送中的订单并改变为自提（分页）
   @PutMapping("/updateOrderStatusBySiteId")
    public Result<PageOrdersDto> updateOrderStatusBySiteId(@RequestBody UpOderStatusParam updateOderStatusParam) throws JsonProcessingException {
       FindOrderBySiteIdParam findOrderBySiteIdParam = new FindOrderBySiteIdParam();
       findOrderBySiteIdParam.setSiteId(updateOderStatusParam.getSiteId());
       findOrderBySiteIdParam.setOrderStatus(updateOderStatusParam.getOrderStatus());
       PageOrdersDto pageOrdersDto = orderAPI.findOrderBySiteId(findOrderBySiteIdParam).getData();
       List<OrderDto> orderDtoList = pageOrdersDto.getOrderDtoList();
       System.out.println(orderDtoList);
       //获取要改变订单的ids
       List<Integer> ids = orderDtoList.stream().map(item -> item.getOrderId()).collect(Collectors.toList());
       UpdateOrderListStatusParam updateOrderListStatusParam = new UpdateOrderListStatusParam();
       updateOrderListStatusParam.setOrderStatus(4);
       updateOrderListStatusParam.setOrderIds(ids);
       orderAPI.updateOrderListStatus(updateOrderListStatusParam);
       return Result.newInstance().setMessage("查询"+updateOderStatusParam.getSiteId()+"配送送中订单成功").setData(pageOrdersDto);

   }


   @Resource
   private FreGoodsAPI freGoodsAPI;
    @Resource
    private BalanceApi balanceApi;
   //退货
      @PutMapping("/updateOrder")
    public  Result updateOrder(@RequestBody UpdateOrderParam updateOrder){
          List<GoodsParam> goodsList = updateOrder.getGoodsList();
          //获取要退货的goodId集合;
          List<Integer> ids = goodsList.stream().map(item -> item.getGoodId()).collect(Collectors.toList());
          Map<Integer, FreGoodsDto> goods = freGoodsAPI.findGoodsInfos(ids).getData();
          //要减去的佣金
          BigDecimal allBalance = new BigDecimal(0);
          for (GoodsParam good : goodsList) {
              BigDecimal goodBalance = goods.get(good.getGoodId()).getBalanceconfPercent().multiply(BigDecimal.valueOf(good.getGoodCount())).multiply(goods.get(good.getGoodId()).getGoodsPrice());
            allBalance=allBalance.add(goodBalance);
          }
          UpdateBalanceParam updateBalanceParam = new UpdateBalanceParam();
          updateBalanceParam.setOrderId(updateOrder.getOrderId());
          updateBalanceParam.setUpdateMoney(allBalance);
          //调用减佣金的api
          Integer code = balanceApi.updateBalance(updateBalanceParam).getCode();
          if (code==200){
          return  Result.newInstance().setMessage("退货成功");
          }
          return  Result.newInstance().setMessage("退货失败");
      }
}
